//
//  WeatherViewController.swift
//  WeatherDemo
//
//  Created by 张峰 on 15/3/4.
//  Copyright (c) 2015年 wdlgame. All rights reserved.
//

import UIKit
import Alamofire

class WeatherViewController: UIViewController {

    var cityName :(cn:String, en:String) = ("","")
    var cityAir: CityAir? {
        
        didSet {
            pm2_5Label.text = "\(cityAir!.pm2_5)"
            qualityLabel.text = "\(cityAir!.quality)"
        }
        
    }
    var bgColor: UIColor?
    
    @IBOutlet weak var pm2_5Label: UILabel!
    @IBOutlet weak var qualityLabel: UILabel!
    @IBOutlet weak var lastUpdateTimeLabel: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = cityName.cn
        
        self.view.backgroundColor = bgColor
        self.navigationController?.navigationBar.barTintColor = bgColor
        pm2_5Label.textColor = UIColor.whiteColor()
        qualityLabel.textColor = UIColor.whiteColor()
        
        self.loadCityPM2_5Request()
        
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func loadCityPM2_5Request() {

        
        var urlString = "http://listenplus.cn:9999/\(cityName.en)"
        
        Alamofire.request(.GET, urlString, parameters: nil, encoding: ParameterEncoding.JSON).responseJSON { (request, response, JSONInfo, error) -> Void in
            if let info: AnyObject = JSONInfo {
                let json = JSON(info)["result"]
                if let object: AnyObject = json.arrayObject?.last {
                    self.cityAir = CityAir(object: object)
                }
            }
        }
    }

    /*
     MARK: - Navigation

     In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
         Get the new view controller using segue.destinationViewController.
         Pass the selected object to the new view controller.
    }
    */

}
